package gui2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;
import javax.swing.filechooser.FileNameExtensionFilter;


public class PanelConfiguration extends JPanel implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final String ACTION_LOAD = "ACTION_LOAD";
	private static final String ACTION_ADD_FIBER_SET = "ACTION_ADD_FIBER";
	private static final String ACTION_REMOVE_FIBER_SET = "ACTION_REMOVE_FIBER";
	private static final String ACTION_PROCESS = "ACTION_PROCESS";
	
	private static final String ACTION_ADD_VOLUME = "ACTION_ADD_VOLUME";
	
	private static final String LINES_ = "LINES_";
	private static final String POINTS_ = "POINTS_";
	private static final String START_POINTS_ = "START_POINTS_";
	private static final String END_POINTS_ = "END_POINTS_";
	private static final String FIBER_COLOR_ = "FIBER_COLOR_";
	private static final String FIBER_RENDERCOUNT_ = "FIBER_RENDER_COUNT_";
	
	private static final String BUTTON_TEXT_LOAD = "LOAD";
	private static final String BUTTON_TEXT_PROCESS = "PROCESS";
	
	private JButton buttonLoadProcess;
	private JButton buttonAddFiber;
	private JButton buttonRemoveFiber;
	private MainFrame mainFrame;
	private ArrayList<FiberSetSetup> fiberSets;
	private ArrayList<VolumeSetup> volumeSets;
	private JPanel panelConfiguration;
	
	public PanelConfiguration(MainFrame mainFrame)
	{
		this.mainFrame = mainFrame;
		fiberSets = new ArrayList<FiberSetSetup>();
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createTitledBorder("Configuration"));
		
		panelConfiguration = new JPanel();
		JPanel panelAuxi = new JPanel(new BorderLayout());
		panelAuxi.add(panelConfiguration, BorderLayout.NORTH);
//		panelAuxi.setPreferredSize(new Dimension(550, 0));
		JScrollPane scrollPane = new JScrollPane(panelAuxi, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		buttonLoadProcess = new JButton(BUTTON_TEXT_LOAD);
		buttonLoadProcess.setActionCommand(ACTION_LOAD);
		buttonLoadProcess.addActionListener(this);
		buttonLoadProcess.setEnabled(false);
		
		buttonAddFiber = new JButton("Add Fiber Set");
		buttonAddFiber.setActionCommand(ACTION_ADD_FIBER_SET);
		buttonAddFiber.addActionListener(this);
		
		buttonRemoveFiber = new JButton("Remove Fiber");
		buttonRemoveFiber.setActionCommand(ACTION_REMOVE_FIBER_SET);
		buttonRemoveFiber.addActionListener(this);
		
		add(scrollPane, BorderLayout.CENTER);
		
		JPanel panelAddRemButtons = new JPanel(new GridLayout(1,2));
		panelAddRemButtons.add(buttonAddFiber);
		panelAddRemButtons.add(buttonRemoveFiber);
		
		JPanel panelButtons = new JPanel(new BorderLayout());
		panelButtons.add(panelAddRemButtons, BorderLayout.NORTH);
		panelButtons.add(buttonLoadProcess, BorderLayout.SOUTH);
		
		add(panelButtons, BorderLayout.SOUTH);
		
	}
	
	public void enableRenderButton(boolean enable)
	{
		buttonLoadProcess.setEnabled(enable);
	}
	
	public void update()
	{
		panelConfiguration.removeAll();
		panelConfiguration.setLayout(new GridLayout(fiberSets.size(), 2));
		
		for(int i = 0; i < fiberSets.size(); i++)
		{
			FiberSetSetup fss = fiberSets.get(i);
			boolean isLoaded = fss.isLoaded();
			boolean[] renderType = fss.getRenderType();
			JPanel panelRender = new JPanel(new GridLayout(1,1));
			
			JLabel labFilePath = new JLabel(fss.getName());
			JLabel labLoaded = new JLabel();
			labLoaded.setOpaque(true);
			labLoaded.setPreferredSize(new Dimension(25, 0));
			if(isLoaded)
			{
				labLoaded.setBackground(Color.GREEN);
			}
			else
			{
				labLoaded.setBackground(Color.YELLOW);
			}
			labLoaded.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			
			JButton butColor = new JButton();
			butColor.setOpaque(true);
			butColor.setBorderPainted(false);
			butColor.setBackground(fss.getColor());
			butColor.setActionCommand(FIBER_COLOR_ + i);
			butColor.addActionListener(this);
			if(isLoaded)
				butColor.setEnabled(false);
			
			JButton butRenderCount = new JButton(""+fss.getRenderCount());
			butRenderCount.setActionCommand(FIBER_RENDERCOUNT_ + i);
			butRenderCount.addActionListener(this);
			if(isLoaded)
				butRenderCount.setEnabled(false);
			
			JPanel panelFiberName = new JPanel(new BorderLayout());
			panelFiberName.add(labLoaded, BorderLayout.WEST);
			panelFiberName.add(labFilePath, BorderLayout.CENTER);
			panelConfiguration.add(panelFiberName);
			
			JCheckBox jcb_lines = new JCheckBox();
			jcb_lines.setActionCommand(LINES_ + i);
			jcb_lines.addActionListener(this);
			jcb_lines.setSelected(renderType[0]);
			
			JCheckBox jcb_points = new JCheckBox();
			jcb_points.setActionCommand(POINTS_ + i);
			jcb_points.addActionListener(this);
			jcb_points.setSelected(renderType[1]);
			
			JCheckBox jcb_startpoints = new JCheckBox();
			jcb_startpoints.setActionCommand(START_POINTS_ + i);
			jcb_startpoints.addActionListener(this);
			jcb_startpoints.setSelected(renderType[2]);
			
			JCheckBox jcb_endpoints = new JCheckBox();
			jcb_endpoints.setActionCommand(END_POINTS_ + i);
			jcb_endpoints.addActionListener(this);
			jcb_endpoints.setSelected(renderType[3]);
			
			panelRender.add(butColor);
			panelRender.add(butRenderCount);
//			panelRender.add(jcb_lines);
//			panelRender.add(jcb_points);
//			panelRender.add(jcb_startpoints);
//			panelRender.add(jcb_endpoints);
			
			panelConfiguration.add(panelRender);
		}
		panelConfiguration.revalidate();
		
		if(fiberSets.size()>0)
		{
			buttonLoadProcess.setEnabled(true);
		}
	}
	
	public void addFiberSetSetup(FiberSetSetup fss)
	{
		if(!fiberSets.contains(fss))
		{
			fiberSets.add(fss);
		}
	}
	

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		String actionCommand = actionEvent.getActionCommand();
		if(actionCommand.equals(ACTION_LOAD))
		{
			mainFrame.loadFiberSetSetups(fiberSets);
			buttonLoadProcess.setText(BUTTON_TEXT_PROCESS);
			buttonLoadProcess.setActionCommand(ACTION_PROCESS);
			buttonAddFiber.setEnabled(false);
			buttonRemoveFiber.setEnabled(false);
		}
		else if(actionCommand.equals(ACTION_PROCESS))
		{
			mainFrame.processFibers();
			buttonLoadProcess.setText(BUTTON_TEXT_LOAD);
			buttonLoadProcess.setActionCommand(ACTION_LOAD);
			buttonAddFiber.setEnabled(true);
			buttonRemoveFiber.setEnabled(true);
		}
		else if(actionCommand.equals(ACTION_ADD_FIBER_SET))
		{
			JFileChooser fileChooser = new JFileChooser(mainFrame.getLastVTKDirectory());
			fileChooser.setFileFilter(new FileNameExtensionFilter("VTK Files", new String[]{"vtk"}));
			fileChooser.setMultiSelectionEnabled(true);
			int option = fileChooser.showOpenDialog(this);
			if(option == JFileChooser.APPROVE_OPTION)
			{
				File[] selectedFiles = fileChooser.getSelectedFiles();
				mainFrame.setLastVTKDirectory(selectedFiles[0].getParent());
				for(File file : selectedFiles)
				{
					String fiberpath = file.getAbsolutePath();
					Random rand = new Random();
					FiberSetSetup newFss = new FiberSetSetup(fiberpath, new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat()), 0, new boolean[]{true, false, false, false});
					if(!fiberSets.contains(newFss))
					{
						fiberSets.add(newFss);
						update();
					}
					
				}
			}
		}
		else if(actionCommand.equals(ACTION_REMOVE_FIBER_SET))
		{
			String[] fiberList = new String[fiberSets.size()];
			for(int i = 0;  i < fiberSets.size(); i++)
			{
				fiberList[i] = fiberSets.get(i).getName();
			}
			String option = (String)JOptionPane.showInputDialog(this, "Select fiber", "", JOptionPane.INFORMATION_MESSAGE, null, fiberList, fiberList[0]);
			if(option != null)
			{
				int i = 0;
				for(i = 0;  i < fiberList.length; i++)
				{
					if(fiberList[i].equals(option))
					{
						fiberSets.remove(i);
						break;
					}
				}
				update();
			}
		}
		else if (actionCommand.contains(FIBER_COLOR_))
		{
			String root = actionCommand.substring(0, actionCommand.lastIndexOf("_") + 1);
			if(root.equals(FIBER_COLOR_))
			{
				int i = Integer.parseInt(actionCommand.substring(actionCommand.lastIndexOf("_") + 1));
				Color newColor = JColorChooser.showDialog(this, "Choose color", fiberSets.get(i).getColor());
				if(newColor != null)
				{
					fiberSets.get(i).setColor(newColor);
					update();
				}
			}
		}
		else if (actionCommand.contains(FIBER_RENDERCOUNT_))
		{
			String root = actionCommand.substring(0, actionCommand.lastIndexOf("_") + 1);
			if(root.equals(FIBER_RENDERCOUNT_))
			{
				int indexOfFiberSet = Integer.parseInt(actionCommand.substring(actionCommand.lastIndexOf("_") + 1));
				int maxFiberCount = fiberSets.get(indexOfFiberSet).getMaximumRenderCount();
				int currentFiberCount = fiberSets.get(indexOfFiberSet).getRenderCount();
				String[] selectionValues = new String[maxFiberCount];
				for(int j = 0; j <maxFiberCount ; j++)
				{
					selectionValues[j] = "" + (j + 1); 
				}
				String selected = (String) JOptionPane.showInputDialog(this, "Choose a number of fibers", "", JOptionPane.INFORMATION_MESSAGE, null, selectionValues, selectionValues[currentFiberCount - 1]);
				if(selected != null)
				{
					int selectedNumber = Integer.parseInt(selected);
					if(maxFiberCount != (selectedNumber + 1))
					{
						fiberSets.get(indexOfFiberSet).setRenderCount(selectedNumber);
						update();
					}
				}
			}
		}
	}

	public ArrayList<FiberSetSetup> getFiberSets() {
		return fiberSets;
	}
	
}
